package com.yxkj.syh.app.huarong.activities.scan;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.lcw.library.imagepicker.ImagePicker;
import com.syh.app.basic.base.BaseActivity;
import com.syh.app.basic.utils.ComUtil;
import com.syh.app.basic.utils.Loog;
import com.syh.app.basic.utils.imagepickerhelper.ImagePickerHelper;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.yxkj.syh.app.huarong.BR;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.databinding.ActivityScanBinding;

import java.io.File;
import java.util.List;

public class ScanActivity extends BaseActivity<ActivityScanBinding, ScanVM> {
    public static final int REQUEST_IMAGE = 0xb11;
    public static final int PERMISSION_IMAGE = 0xb12;
    public static final int REQUEST_CODE_SCAN = 0xb13;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_scan;
    }

    @Override
    protected int getViewModelId() {
        return BR.scanVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        /**
         * 执行扫面Fragment的初始化操作
         */
        CaptureFragment captureFragment = new CaptureFragment();
//        // 为二维码扫描界面设置定制化界面
//        CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera);

        captureFragment.setAnalyzeCallback(analyzeCallback);
        /**
         * 替换我们的扫描控件
         */
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container, captureFragment).commit();


        mVDBinding.titleView.setLeftOnClickListener(v -> finish());
        mVDBinding.titleView.setRightOnClickListener(v -> {
            startImageLib();
        });
    }

    private void startImageLib() {
        ImagePickerHelper.startImagePicker(this);
    }

    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
            bundle.putString(CodeUtils.RESULT_STRING, result);
            resultIntent.putExtras(bundle);
            ScanActivity.this.setResult(RESULT_OK, resultIntent);
            ScanActivity.this.finish();
        }

        @Override
        public void onAnalyzeFailed() {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
            bundle.putString(CodeUtils.RESULT_STRING, "");
            resultIntent.putExtras(bundle);
            ScanActivity.this.setResult(RESULT_OK, resultIntent);
            ScanActivity.this.finish();
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ImagePickerHelper.REQUEST_SELECT_IMAGES_CODE) {
            if (resultCode == RESULT_OK) {
                List<String> imagePaths = data.getStringArrayListExtra(ImagePicker.EXTRA_SELECT_IMAGES);
                try {
                    CodeUtils.analyzeBitmap(imagePaths.get(0), analyzeCallback);
                } catch (Exception e) {
                    Loog.e("ScanActivity", e.getMessage());
                }
            }
        }
    }
}
